import {Pipe, PipeTransform} from '@angular/core';

/**
 * Generated class for the MyPipe pipe.
 * 对金钱数字的格式化
 *
 */
@Pipe({
  name: 'distance',
})
export class DistancePipe implements PipeTransform {
  transform(value, type?, jiange?, xsleng?, wlength?) {
    if (!type) {
      type = "Km"
    }

    return this.getMoneyType(parseFloat(value) / 1000 + '', wlength, jiange, xsleng) + type;
  }

  /**
   * 对金钱数据的格式化 例：4，455，455，555.45 ￥
   * @param {string} value 需要转化的元数据
   * @param {number} wlength 间隔的长度
   * @param {string} jiange 间隔的符号，例","
   * @param {number} xsleng 小数位的长度
   * @returns {string} 最终返回的个是字符
   */
  getMoneyType(value: string, wlength?: number, jiange?: string, xsleng?: number): string {
    let yshuzi = parseFloat(value);
    if (!wlength) {
      wlength = 3;
    }
    if (!jiange) {
      jiange = ',';
    }
    if (!xsleng) {
      xsleng = 1;
    }
    if (!isNaN(yshuzi)) {
      let b;
      if (yshuzi >= 0) {
        b = '';
      } else {
        b = '-';
      }
      let strxs: string = '';
      if ((value + '').indexOf(".") != -1) {
        strxs = "." + (value + '').substr((value + '').indexOf(".") + 1, xsleng);
      } else {
        strxs = ".0"
      }
      let zhshnum: number = parseInt(value, 10)//取整数，十进制
      let zhshlength: number = (Math.abs(zhshnum) + '').length;//整数位的长度
      if (zhshlength > wlength) {
        let jjj = Math.floor(zhshlength / wlength);//几个长度
        let jj = zhshlength % wlength;  //多余的
        let strz: string = Math.abs(zhshnum) + '';//
        let tr = strz.substr(0, jj);
        for (let i = 0; i < jjj; i++) {
          tr += ',' + strz.substr(jj + i * wlength, wlength);
        }
        let ty = tr.replace(/^,/g, "");
        return (b + ty + strxs).replace(/--/g, '-');
      } else {
        return (b + zhshnum + strxs).replace(/--/g, '-');
      }
    } else {
      if (!value) {
        return "0.0"
      }
      return "不是数字格式！"
    }
  }

}


@Pipe({
  name: 'toapam',
})
export class MyToapamPipe implements PipeTransform {
  transform(date: string) {
    if (date == "0") {
      return "上午";
    }
    if (date == "1") {
      return "下午";
    }
    if (date == "2") {
      return "全天";
    }
    return "";
  }
}

@Pipe({
  name: 'character',
})
export class CharacterPipe implements PipeTransform {
  transform(value: string) {
    if (value) {
      return value.slice(value.length - 2);
    }
  }
}

